#include "miscellaneous.h"
#include "image.h"
#include "morphology.h"
#include "core.h"
#include <iostream>

//Find terminations and dendritic source points simultaneuosly
Binary_Image* findExtremitiesFromSkel(Binary_Image *img,Binary_Image *out){
    Binary_Image* extremities[8];
    //JORGE's comment
    //The result of each hit-or-miss operation through a rotated version "i" of the
    //template "E" is stored in a matrix extremity[i]
    for(int i=0;i<8;i++)
    extremities[i]=MOperator::hit_or_miss_golay(img,NULL,(char*)"E",i);

    //JORGE's comment
    //Making a UNION of the eight previous results and storing it in extremity[0]
    for(int i=1;i<8;i++)
        extremities[0]->OR(extremities[i]);

    //JORGE's comment
    //And now deleting unnecessary results
    for(int i=1;i<8;i++)
        delete extremities[i];

    //JORGE's comment
    //Providing final result as required
    if(out==NULL)
        return extremities[0];
    else{
        for(unsigned int k=0;k<out->getHeight();k++)
            for(unsigned int l=0;l<out->getWidth();l++)
                out->pixelmap[k][l]=extremities[0]->pixelmap[k][l];
        delete extremities[0];
        return out;
        }
}




Grey_Image* density(Binary_Image* img,Grey_Image* out,unsigned int samplesizex,unsigned int samplesizey){
    Grey_Image *temp = new Grey_Image(img->getHeight(),img->getWidth(),samplesizex*samplesizey);

    unsigned int k;

    for(unsigned int i=samplesizey/2;i<(img->getHeight()-samplesizey/2);i+=samplesizey/2)
        for(unsigned int j=samplesizex/2;j<(img->getWidth()-samplesizex/2);j+=samplesizex/2){
            k=0;
            for(int l=-(samplesizey/2);l<(signed)samplesizey/2;l++)
                for(int m=-(samplesizex/2);m<(signed)samplesizex/2;m++){
                    if(img->pixelmap[i+l][j+m]==true){
                        k++;
                    }
                }
            temp->setPixel(j,i,k);
        }

    if(out==NULL)
        return temp;
    else{
        for(unsigned int k=0;k<out->getHeight();k++)
            for(unsigned int l=0;l<out->getWidth();l++)
                out->pixelmap[k][l]=temp->pixelmap[k][l];
        delete temp;
        return out;
        }
}

